﻿/*
 * 进行两个多项式的加法运算
 */

int ITEMS = 6;
int[] PolyA =  { 4, 3, 7, 0, 6, 2 };
int[] PolyB = { 4, 1, 5, 2, 0, 9 };
Console.Write("多项式A=>");
PrintPoly(PolyA,ITEMS);
Console.Write("多项式B=>");
PrintPoly(PolyB,ITEMS);
Console.Write("A + B =>");
PolySum(PolyA,PolyB);
Console.ReadKey();


static void PrintPoly(int[] Poly, int items)
{
    int i, MaxExp;
    MaxExp = Poly[0];
    for (i = 1; i <= Poly[0] + 1; i++)
    {
        MaxExp--;
        if (Poly[i] != 0)
        {
            if ((MaxExp + 1) != 0)
            {
                Console.Write(Poly[i] + "X^" + (MaxExp + 1));
            }
            else
            {
                Console.Write(Poly[i]);
            }
        }
    }

    Console.WriteLine();
}

static void PolySum(int[] Poly1, int[] Poly2)
{
    int i;
    int ITEMS = 6;
    int[] result = new int[ITEMS];
    result[0] = Poly1[0];
    for (i = 1; i <= Poly1[0] + 1; i++)
    {
        result[i] = Poly1[i] + Poly2[i];
    }

    PrintPoly(result, 6);
}